package com.book.controller;

import com.book.po.Admin;
import com.book.po.Book;
import com.book.po.BookType;
import com.book.po.ReaderType;
import com.book.service.adminService;
import com.book.service.bookTypeService;
import com.book.service.readerTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminController {
    @Autowired
    private adminService adminservice;
    @Autowired
    private readerTypeService readerTypeService;
    @Autowired
    private bookTypeService bookTypeService;
    @RequestMapping("/login")
    public String login(Model model, HttpSession session, Admin admin){
        Admin admin_t = adminservice.login(admin);
        List<ReaderType> readerTypes = readerTypeService.readerType_select();
        List<BookType> bookTypes = bookTypeService.bookType_select("");
        if (admin_t!=null){
            session.setAttribute("admin",admin_t);
            session.setAttribute("readerTypes",readerTypes);
            session.setAttribute("bookTypes",bookTypes);
            model.addAttribute("msg","");
            return "admin.html";
        }else {
            model.addAttribute("msg","账号或密码错误");
            return "adminLogin.html";
        }

    }

    @RequestMapping("/quit")
    public String quit(RedirectAttributes  model,HttpSession session){
        session.removeAttribute("admin");
        session.removeAttribute("bookTypes");
        session.removeAttribute("readerTypes");
        return "adminLogin.html";
    }
    @RequestMapping("/admin_select")
    public String admin_select(RedirectAttributes model, Admin admin){
        List<Admin> admins = adminservice.admin_select(admin);
        if (admins.size()>=1){
            model.addFlashAttribute("admins",admins);
            model.addFlashAttribute("msg","");
            return "redirect:/manage/adminManage";
        }else {
            model.addFlashAttribute("msg","没有查找到对应管理员！");
            return "redirect:/manage/adminManage";
        }
    }


    @RequestMapping("/admin_insert")
    public String admin_insert(RedirectAttributes  model, Admin admin){
        String msg = adminservice.admin_insert(admin);
        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/adminManage";
    }

    @RequestMapping("/admin_update")
    public String admin_update(RedirectAttributes  model, Admin admin){
        String msg = adminservice.admin_update(admin);
        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/adminManage";
    }

    @RequestMapping("/admin_delete")
    public String book_delete(RedirectAttributes  model, Integer aid){
        String msg = adminservice.admin_delete(aid);
        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/adminManage";
    }
}
